"""
 -*- coding: utf-8-*-
 @File: api_project.py
 @Author: chen
 @time: 2023/12/02/ 9:03
"""
from stage_L1.lim_project.api_po.businesses.api_home import HomeApi
from stage_L1.lim_project.api_po.utils.mgt_logger import logger


class ProjectMgtApi(HomeApi):
    def _project_envir_data(self):
        """项目环境信息接口"""
        path = "/project/project-envir-data"
        resp = self.send("GET", path)
        return resp.json()

    def create(self, project_name):
        """
        创建项目接口
        :param project_name: 项目名称
        :return:
        """
        logger.info(rf"创建项目【{project_name}】")
        path = "/project/project-view"
        data = {"name": project_name}
        self.payload = {"body": data}
        resp = self.send("POST", path, json=data)
        return resp.json()

    def update(self, old_name, new_name):
        """
        修改项目接口
        :param old_name: 旧项目名称
        :param new_name: 新项目名称
        :return:
        """
        logger.info(rf"修改项目【{old_name}】为【{new_name}】")
        path = "/project/project-view"
        project_id = self.get_project_id_by_name(old_name)
        data = {"name": new_name, "id": project_id}
        self.payload = {"body": data}
        resp = self.send("PATCH", path, json=data)
        return resp.json()

    def delete(self, project_name):
        """
        删除项目接口
        :param project_name: 要删除项目的名称
        :return:
        """
        logger.info(fr"删除项目【{project_name}】")
        path = "/project/project-view"
        project_id = self.get_project_id_by_name(project_name)
        if project_id:
            data = f"id={project_id}"
            self.payload = {"params": data}
            resp = self.send("DELETE", path, params=data)
            return resp.json()
        else:
            return None

    def _delete_by_id(self, project_id):
        logger.info(fr"删除项目【{project_id}】")
        path = "/project/project-view"
        # project_id = self.get_project_id_by_name(project_name)
        data = f"id={project_id}"
        self.payload = {"params": data}
        resp = self.send("DELETE", path, params=data)
        return resp.json()


if __name__ == '__main__':
    p0 = ProjectMgtApi()
    project_name0 = "@"
    # print(p0.get_project_names())
    # print(p0.get_project_id_by_name("\@"))
    print(p0.create(project_name0))
    new_name0 = "cc_project_1202_01_new"
    print(p0.update(project_name0, new_name0))
    print(p0.delete(new_name0))
